using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using SmartLink.DTOS.JsonSettings;

namespace SmartLink.DTOS
{
    /// <summary>
    /// 日期范围参数
    /// </summary>
    public class DateRangePara
    {
        private DateTime startDate;
        private DateTime endDate;

        /// <summary>
        /// 开始日期
        /// </summary>
        [JsonConverter(typeof(CustomDateTimeConverter))]
        public DateTime StartDate
        {
            get { return startDate; }
            set
            {
                if (value.Kind == DateTimeKind.Utc) startDate = value.ToLocalTime();
                else startDate = value;
            }
        }
        /// <summary>
        /// 结束日期
        /// </summary>
        [JsonConverter(typeof(CustomDateTimeConverter))]
        public DateTime EndDate
        {
            get { return endDate; }
            set
            {
                if (value.Kind == DateTimeKind.Utc) endDate = value.ToLocalTime();
                else endDate = value;
            }
        }

        /// <summary>
        /// 用于SQL的日期查询截至日期，自动在用户请求的EndDate上加一天，即表示用户需要包含EndDate一整天的范围
        /// </summary>
        [IgnoreDataMember]
        public DateTime EndDate_Sql
        {
            get { return endDate.AddDays(1); }
        }
    }

    /// <summary>
    /// 可空的日期范围参数
    /// </summary>
    public class DateRangeNullablePara
    {
        private DateTime? startDate;
        private DateTime? endDate;

        /// <summary>
        /// 开始日期
        /// </summary>
        public DateTime? StartDate
        {
            get { return startDate; }
            set
            {
                if (value.HasValue && value.Value.Kind == DateTimeKind.Utc) startDate = value.Value.ToLocalTime();
                else startDate = value;
            }
        }
        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTime? EndDate
        {
            get { return endDate; }
            set
            {
                if (value.HasValue && value.Value.Kind == DateTimeKind.Utc) endDate = value.Value.ToLocalTime();
                else endDate = value;
            }
        }

        /// <summary>
        /// 用于SQL的日期查询截至日期，自动在用户请求的EndDate上加一天，即表示用户需要包含EndDate一整天的范围
        /// </summary>
        [IgnoreDataMember]
        public DateTime? EndDate_Sql
        {
            get { return endDate.HasValue ? new DateTime?(endDate.Value.AddDays(1)) : null; }
        }
    }

    /// <summary>
    /// 日期范围分页查询
    /// </summary>
    public class DateRangePagePara : PageBase
    {
        private DateTime startDate;
        private DateTime endDate;

        /// <summary>
        /// 开始日期
        /// </summary>
        [JsonConverter(typeof(CustomDateTimeConverter))]
        public DateTime StartDate
        {
            get { return startDate; }
            set
            {
                if (value.Kind == DateTimeKind.Utc) startDate = value.ToLocalTime();
                else startDate = value;
            }
        }
        /// <summary>
        /// 结束日期
        /// </summary>
        [JsonConverter(typeof(CustomDateTimeConverter))]
        public DateTime EndDate
        {
            get { return endDate; }
            set
            {
                if (value.Kind == DateTimeKind.Utc) endDate = value.ToLocalTime();
                else endDate = value;
            }
        }

        /// <summary>
        /// 用于SQL的日期查询截至日期，自动在用户请求的EndDate上加一天，即表示用户需要包含EndDate一整天的范围
        /// </summary>
        [IgnoreDataMember]
        public DateTime EndDate_Sql
        {
            get { return endDate.AddDays(1); }
        }
    }

    /// <summary>
    /// 日期范围带关键字分页查询
    /// </summary>
    public class DateRangeKeyPagePara : DateRangePagePara
    {
        /// <summary>
        /// 关键字
        /// </summary>
        public string KeyValue { get; set; }
    }

    /// <summary>
    /// 可空的日期范围分页查询
    /// </summary>
    public class DateRangeNullablePagePara : PageBase
    {
        private DateTime? startDate;
        private DateTime? endDate;

        /// <summary>
        /// 开始日期
        /// </summary>
        [JsonConverter(typeof(CustomDateTimeConverter))]
        public DateTime? StartDate
        {
            get { return startDate; }
            set
            {
                if (value.HasValue && value.Value.Kind == DateTimeKind.Utc) startDate = value.Value.ToLocalTime();
                else startDate = value;
            }
        }
        /// <summary>
        /// 结束日期
        /// </summary>
        [JsonConverter(typeof(CustomDateTimeConverter))]
        public DateTime? EndDate
        {
            get { return endDate; }
            set
            {
                if (value.HasValue && value.Value.Kind == DateTimeKind.Utc) endDate = value.Value.ToLocalTime();
                else endDate = value;
            }
        }

        /// <summary>
        /// 用于SQL的日期查询截至日期，自动在用户请求的EndDate上加一天，即表示用户需要包含EndDate一整天的范围
        /// </summary>
        [IgnoreDataMember]
        public DateTime? EndDate_Sql
        {
            get { return endDate.HasValue ? new DateTime?(endDate.Value.AddDays(1)) : null; }
        }
    }

    /// <summary>
    /// 日期范围带关键字分页查询
    /// </summary>
    public class DateRangeKeyNullablePagePara : DateRangeNullablePagePara
    {
        /// <summary>
        /// 关键字
        /// </summary>
        public string KeyValue { get; set; }
    }
}
